home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: realloc question
- Date: 03 Feb 1996 17:16:26 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb3101626@qcd.lanl.gov>
- References: <4ehi4b$qfo@news.texas.net> <DM5tEn.BFo@eskimo.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: scs@eskimo.com's message of Fri, 2 Feb 1996 17:47:10 GMT
-
- In article <DM5tEn.BFo@eskimo.com> scs@eskimo.com (Steve Summit)
- writes:
- <snip>
- > ptr=(struct sumthin**) realloc(ptr,(x+1) * sizeof(struct sumthin));
- <snip>
- and wasting (not using) most of it. The second line
- should read
-
- ptr = (struct sumthin **)realloc(ptr,
- (x+1) * sizeof(struct sumthin *));
-
- (This is easy to spot, if you remember the rule that
- there should always be one more * in the cast than in the
- sizeof. On the other hand, the explicit cast isn't
- really recommended any more.)
-
- Actually, in these cases, the form that I personally like is
-
- ptr1 = realloc(ptr2, (x+1) * sizeof *ptr2)
-
- The sizeof *ptr2 forces the correct size irrespective of what type it
- is. Of course, things like malloc, calloc and realloc are not really
- type safe: the above for example does not check that ptr1 and ptr2 are
- indeed the same type. But usually I use it in the context:
-
- if ( (ptr1 = realloc(ptr2, (x+1) * sizeof *ptr2)) ) ptr2 = ptr1;
-
- and do not use ptr1 beyond that. This brings back the type safety.
-
- (and yes, I do not always distinguish the boolean concept true/false
- from integer or pointer concepts 0, NULL especially when the 0
- etc. indicate failure. I tend to put extra parentheses in some of
- these cases as an indication to some compilers that there is no typo.)
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-